/*
 * 作者:熊猫
 * 日期:2017 - 3 - 20 10 - 22 - 52
 *
 */

package net.lainiao.morethread.Chapter8.demo2;

import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/20.
 */
public class Task implements Runnable {
    private int time;
    private Phaser phaser;

    public Task(int time, Phaser phaser) {
        this.time = time;
        this.phaser = phaser;
    }

    @Override
    public void run() {
        phaser.arrive();
        System.out.println(Thread.currentThread().getName()+": Entering phase 1");
        try {
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+": Finishing phase 2");
        phaser.arriveAndAwaitAdvance();
        String thName=Thread.currentThread().getName();
        System.out.println(thName+" Entering phase 3");
        try {
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(thName+" : Finishing phase ");
        phaser.arriveAndDeregister();
    }
}
